package com.wl156.activities.order;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.InputFilter.LengthFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.wl156.MyApp;
import com.wl156.R;
import com.wl156.activities.Wl156Activity;
import com.wl156.activities.find.CarMessageInfo;
import com.wl156.activities.find.FindCar;
import com.wl156.activities.find.FindCarMessage;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.utils.Utils;

public class MyGoodInfo extends Activity implements OnClickListener {

	private TextView mProvice;
	private TextView mCity;
	private TextView mRegion;
	private TextView mToProvice;
	private TextView mToCity;
	private TextView mToRegion;
	private TextView mName;
	private TextView mWeight;
	private TextView mLength;
	private TextView mContact;
	private TextView mPhone;
	private TextView mNote;
	private TextView mNeedCarModelText;
	private TextView mNeddCarCount;
	private TextView mSetOffDate;

	private ImageButton mCall;
	private Button mCollect;
	private Button mSelectDriver;
	private Button mChengjiao;

	private long mGoodId;

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_good_info);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		mGoodId = getIntent().getLongExtra("id", 0);

		mName = (TextView) findViewById(R.id.name);
		mWeight = (TextView) findViewById(R.id.weight);
		mLength = (TextView) findViewById(R.id.length);
		mContact = (TextView) findViewById(R.id.contact);
		mPhone = (TextView) findViewById(R.id.phone);
		mNote = (TextView) findViewById(R.id.note);
		mProvice = (TextView) findViewById(R.id.provice);
		mToProvice = (TextView) findViewById(R.id.to_provice);
		mNeedCarModelText = (TextView) findViewById(R.id.need_car_model_text);
		mNeddCarCount = (TextView) findViewById(R.id.need_car_count);

		mSetOffDate = (TextView) findViewById(R.id.setOffDate);

		mCall = (ImageButton) findViewById(R.id.call);
		mCall.setOnClickListener(this);
		mCollect = (Button) findViewById(R.id.collect);
		mCollect.setOnClickListener(this);
		mSelectDriver = (Button) findViewById(R.id.select_driver);
		mSelectDriver.setOnClickListener(this);
		mSelectDriver.setVisibility(View.VISIBLE);
		mChengjiao = (Button) findViewById(R.id.chengjiao);
		mChengjiao.setOnClickListener(this);
		mChengjiao.setVisibility(View.VISIBLE);

		new AsyncTask<Void, Void, Object>() {
			ProgressDialog mDialog;

			@Override
			protected void onPostExecute(Object result) {
				if (result instanceof SoapObject) {
					SoapObject so = (SoapObject) result;
					String id = Utils.propertyToShow(so.getProperty(0));
					String start = Utils.propertyToShow(so.getProperty(1));
					String end = Utils.propertyToShow(so.getProperty(2));
					String companyName = Utils
							.propertyToShow(so.getProperty(3));
					String goodName = Utils.propertyToShow(so.getProperty(4));
					String weight = Utils.propertyToShow(so.getProperty(5));
					String needType = Utils.propertyToShow(so.getProperty(6));
					String needLength = Utils.propertyToShow(so.getProperty(7));
					String contact = Utils.propertyToShow(so.getProperty(8));
					String phone = Utils.propertyToShow(so.getProperty(9));
					String internalPhone = Utils.propertyToShow(so
							.getProperty(10));
					String setdateoff = Utils
							.propertyToShow(so.getProperty(11));
					String note = Utils.propertyToShow(so.getProperty(12));
					String needCarCount = Utils.propertyToShow(so
							.getProperty(13));
					String creditScore = Utils.propertyToShow(so
							.getProperty(14));
					String finishOrderCount = Utils.propertyToShow(so
							.getProperty(15));
					mName.setText(goodName);
					mProvice.setText(start);
					mToProvice.setText(end);
					mWeight.setText(weight);
					mNeedCarModelText.setText(needType);
					mNeddCarCount.setText(needCarCount);
					mLength.setText(needLength);
					mContact.setText(contact);
					mPhone.setText(phone);
					mNote.setText(note);
					mSetOffDate.setText(setdateoff);
					
					MyApp.sGoodInfo.id = id;
					MyApp.sGoodInfo.from = start;
					MyApp.sGoodInfo.to = end;
					MyApp.sGoodInfo.name = goodName;
					MyApp.sGoodInfo.company = companyName;
					MyApp.sGoodInfo.weight = weight;
					MyApp.sGoodInfo.length = needLength;
					MyApp.sGoodInfo.needLength = needLength;
					MyApp.sGoodInfo.contact = contact;
					MyApp.sGoodInfo.phone = phone;
					MyApp.sGoodInfo.carType = needType;
					MyApp.sGoodInfo.startDate = setdateoff;
				} else {
					Toast.makeText(MyGoodInfo.this, "获取信息失败，请稍候再试",
							Toast.LENGTH_LONG).show();
				}
				mDialog.dismiss();
			};

			@Override
			protected void onPreExecute() {
				mDialog = ProgressDialog.show(MyGoodInfo.this, "", "加载中");
			};

			@Override
			protected Object doInBackground(Void... params) {
				try {
					return mWebServiceClient.commonFindFreightMessageDetail(
							mCertificate, mGoodId);
				} catch (IOException e) {
					e.printStackTrace();
					return e;
				} catch (XmlPullParserException e) {
					e.printStackTrace();
					return e;
				}
			}
		}.execute();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.chengjiao: {
			new AsyncTask<Void, Void, Object>() {
				ProgressDialog mDialog;

				@Override
				protected void onPreExecute() {
					mDialog = ProgressDialog.show(MyGoodInfo.this, "", "加载中");
				};

				@Override
				protected void onPostExecute(Object result) {
					mDialog.dismiss();
					Toast.makeText(MyGoodInfo.this, "成功", Toast.LENGTH_LONG)
							.show();

				}

				@Override
				protected Object doInBackground(Void... params) {
					try {
						return mWebServiceClient.company_CargosInfo(
								mCertificate, String.valueOf(mGoodId));
					} catch (IOException e) {
						e.printStackTrace();
						return e;
					} catch (XmlPullParserException e) {
						e.printStackTrace();
						return e;
					}
				}
			}.execute();
		}
			break;
		case R.id.select_driver: {
			Intent i = new Intent(MyGoodInfo.this, FindCarMessage.class);
			i.putExtra("order_select", true);
			startActivity(i);
		}
			break;
		case R.id.call: {
			Intent i = new Intent(Intent.ACTION_DIAL);
			i.setData(Uri.parse("tel:" + mPhone.getText().toString()));
			startActivity(i);
		}
			break;
		case R.id.collect:
			new AsyncTask<Void, Void, Object>() {
				ProgressDialog mDialog;

				@Override
				protected void onPreExecute() {
					mDialog = ProgressDialog.show(MyGoodInfo.this, "", "加载中");
				};

				@Override
				protected void onPostExecute(Object result) {
					if (result instanceof SoapObject) {
						SoapObject so = (SoapObject) result;
						final String[] ids = new String[so.getPropertyCount()];
						String[] names = new String[so.getPropertyCount()];
						String[] counts = new String[so.getPropertyCount()];
						for (int i = 0; i < so.getPropertyCount(); i++) {
							SoapObject line = (SoapObject) so.getProperty(i);
							String id = line.getPropertyAsString(0);
							ids[i] = id;
							String name = line.getPropertyAsString(1);
							names[i] = name;
							String count = line.getPropertyAsString(2);
							counts[i] = count;
						}

						AlertDialog.Builder builer = new Builder(
								MyGoodInfo.this);
						builer.setItems(names,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											final int which) {
										new AsyncTask<Void, Void, Object>() {
											ProgressDialog mDialog;

											@Override
											protected void onPreExecute() {
												mDialog = ProgressDialog.show(
														MyGoodInfo.this, "",
														"添加中");
											};

											@Override
											protected void onPostExecute(
													Object result) {
												if (result instanceof SoapObject) {
													SoapObject so = (SoapObject) result;
													Toast.makeText(
															MyGoodInfo.this,
															"收藏成功",
															Toast.LENGTH_LONG)
															.show();
												} else if (result instanceof SoapPrimitive) {
													Toast.makeText(
															MyGoodInfo.this,
															result.toString(),
															Toast.LENGTH_LONG)
															.show();
												} else {
													Toast.makeText(
															MyGoodInfo.this,
															"网络异常，添加失败，请稍候再试",
															Toast.LENGTH_LONG)
															.show();
												}
												mDialog.dismiss();
											};

											@Override
											protected Object doInBackground(
													Void... params) {
												try {
													return mWebServiceClient
															.driverModifyStoreGroupAddFreightMessageToGroup(
																	mCertificate,
																	String.valueOf(mGoodId),
																	ids[which]);
												} catch (IOException e) {
													return e;
												} catch (XmlPullParserException e) {
													return e;
												}
											}
										}.execute();
									}
								});
						builer.show();
					} else {
						Toast.makeText(MyGoodInfo.this, "联网失败，请稍候再试",
								Toast.LENGTH_LONG).show();
					}
					mDialog.dismiss();
				};

				@Override
				protected Object doInBackground(Void... params) {
					try {
						return mWebServiceClient
								.commonGetMyStoreGroups(mCertificate);
					} catch (IOException e) {
						return e;
					} catch (XmlPullParserException e) {
						return e;
					}
				}
			}.execute();
			break;
		}
	}
}
